package live.ifox.memo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.Arrays;
import java.util.List;

import live.ifox.entity.Record;
import live.ifox.helper.CommonHelper;
import live.ifox.util.CommonUtil;
import live.ifox.util.DateUtil;

public class MemoEditor extends AppCompatActivity {
    boolean updateFlag;
    Spinner sp_categories;
    List<String> categories;
    EditText in_category, in_detail;
    String _uid, _category, _detail;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memo_editor);
        actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.setDisplayHomeAsUpEnabled(true);

        updateFlag = false;

        in_category = findViewById(R.id.category);
        in_detail = findViewById(R.id.detail);

        sp_categories = findViewById(R.id.sp_categories);
        sp_categories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (!categories.isEmpty())
                    in_category.setText(categories.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        String[] array = getIntent().getStringExtra("categories").split(",");
        categories = Arrays.asList(array);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item);
        adapter.addAll(categories);
        sp_categories.setAdapter(adapter);

        _uid = getIntent().getStringExtra("uid");
        if (_uid != null && !_uid.isEmpty()) {
            updateFlag = true;
            _category = getIntent().getStringExtra("category");
            _detail = getIntent().getStringExtra("detail");

            if (categories.contains(_category))
                sp_categories.setSelection(categories.indexOf(_category));
            in_detail.setText(_detail);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, ID.ACTION_INSERT_DATE, 0, "插入日期");
        menu.add(0, ID.ACTION_INSERT_TIME, 1, "插入时间");

        SubMenu toolMenu = menu.addSubMenu(0, ID.SUB_MENU, 2, "其他");
        toolMenu.add(1, ID.ACTION_INSERT_UUID, 0, "插入UUID");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case ID.ACTION_INSERT_DATE:
                in_detail.append(DateUtil.getDateString());
                return true;
            case ID.ACTION_INSERT_TIME:
                in_detail.append(DateUtil.getTimeString());
                return true;
            case ID.ACTION_INSERT_UUID:
                in_detail.append(CommonUtil.getUid());
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void onClean_Click(View view) {
        in_category.setText("");
        in_detail.setText("");
    }

    public void onSubmit_Click(View view) {
        String category = in_category.getText().toString();
        String detail = in_detail.getText().toString();
        if (category.isEmpty())
            category = "备忘";

        if (detail.isEmpty()) {
            CommonHelper.showMessage(this, "内容不能为空！");
            return;
        }

        Record record = new Record();
        record.setCategory(category);
        record.setDate_(DateUtil.getDateString());
        record.setDetail(detail);
        if (!updateFlag) {
            record.setUid(CommonUtil.getUid());
        } else {
            if (category.equals(_category) && detail.equals(_detail))
                return;

            record.setUid(_uid);
        }

        Intent intent = new Intent();
        intent.putExtra("record", record);
        setResult(ID.RESULT_DONE, intent);
        finish();
    }
}
